﻿/**
 * @file deque_ie3.c
 * Copyright (c) 2021 Gaaagaa. All rights reserved.
 * 
 * @author  : Gaaagaa
 * @date    : 2021-06-02
 * @version : 1.0.0.0
 * @brief   : 对 双端队列 容器的数据进行 插入/删除 操作。
 */

/**
 * The MIT License (MIT)
 * Copyright (c) Gaaagaa. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "std_deque.h"
#include "std_mdebug.h"
#include <stdio.h>
#include <memory.h>

////////////////////////////////////////////////////////////////////////////////

/**********************************************************/
/**
 * @brief 打印输出（user_t 类型）双端队列容器对象的 各个节点信息。
 */
xstd_void_t deque_int32_print(xdeq_this_t xdeq_int32)
{
    xstd_size_t xst_count = 0;
    xdeq_iter_t xdeq_iter = std_deque_begin(xdeq_int32);
    xdeq_iter_t xdeq_vend = std_deque_end(xdeq_int32);

    if (std_deque_size(xdeq_int32) <= 0)
    {
        printf("deque_int32(0) \n");
        return;
    }

    printf("deque_int32(%d) [[[\n", (xstd_int32_t)std_deque_size(xdeq_int32));

    for (xst_count  = 0;
         xdeq_iter != xdeq_vend;
         xdeq_iter  = std_deque_next(xdeq_int32, xdeq_iter))
    {
        printf(
            "%2d. (%2d)\n",
            (xstd_int32_t)xst_count,
            std_deque_elem_int32(xdeq_int32, xdeq_iter));

        if (++xst_count == std_deque_size(xdeq_int32))
        {
            printf("]]] deque_int32\n\n");
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

/**********************************************************/
/**
 * @brief 测试程序的 main 入口函数。
 */
int main(int argc, char * argv[])
{
    xmdb_this_t xmdb_comm  = XSTD_NULL;
    xstd_int32_t  xit_iter   = 0;
    xdeq_this_t xdeq_int32 = XSTD_NULL;

    //======================================

    xmdb_comm = (xmdb_this_t)std_mdbcomm_start();
    printf("%d. mem count : %d, sizeof(xstd_deque_t) = %d\n",
           __LINE__,
           (xstd_int32_t)std_mdbcomm_count(),
           (xstd_int32_t)std_deque_sizeof());

    //======================================
    // initial

    printf("initial: \n");

    xdeq_int32 = std_deque_create_int32();
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert begin

    printf("insert begin: \n");

    std_deque_insert_int32(xdeq_int32, std_deque_begin(xdeq_int32), 0);
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert end

    printf("insert end: \n");

    std_deque_insert_int32(xdeq_int32, std_deque_end(xdeq_int32), 1);
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert rbegin

    printf("insert rbegin: \n");

    std_deque_insert_n_int32(xdeq_int32, std_deque_rbegin(xdeq_int32), 23, 2);
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert head

    printf("insert head: \n");

    std_deque_insert_n_int32(xdeq_int32, std_deque_iter(xdeq_int32, 1), 5, 3);
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // insert tail

    printf("insert tail: \n");

    std_deque_insert_n_int32(xdeq_int32, std_deque_iter(xdeq_int32, std_deque_size(xdeq_int32) - 2), 7, 4);
    deque_int32_print(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================
    // clear

    printf("clear: \n");

    std_deque_clear(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());

    //======================================

    std_deque_destroy(xdeq_int32);

    printf("%d. mem count : %d\n\n", __LINE__, (xstd_int32_t)std_mdbcomm_count());
    std_mdbcomm_stop();

    //======================================

    return 0;
}

/////////////////////////////////////////////////////////////////////////////////

